/**
 * Desenvolvido pela Fábrica de Software do CESUPA.
 * Todos os direitos reservados.
 */
package br.cesupa.fabsoft.nomedoprojeto.system.application.facades;

import br.cesupa.fabsoft.nomedoprojeto.system.application.ApplicationConfiguration;
import br.cesupa.fabsoft.nomedoprojeto.system.application.ApplicationDefaults;
import br.cesupa.fabsoft.nomedoprojeto.system.application.beans.ApplicationBean;
import br.cesupa.fabsoft.nomedoprojeto.system.infra.i18n.TranslationService;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;
import javax.faces.context.FacesContext;

/**
 * Handle internacionalization of messages, words and related string based
 * content.
 *
 * @author Breno Leite
 */
public class TranslationFacade {

    public static final String FACES_MESSAGE_RESOURCE_BUNDLE_PATH =
            "br.cesupa.fabsoft.nomedoprojeto.system.infra.i18n.facesmessages.faces-messages";
    public static final String WORDS_RESOURCE_BUNDLE_PATH =
            "br.cesupa.fabsoft.nomedoprojeto.system.infra.i18n.words.words";
    public static final String APPLICATION_STATUS_BUNDLE_PATH =
            "br.cesupa.fabsoft.nomedoprojeto.system.infra.i18n.appstatusmessages.app-status-messages";

    /**
     * Translates a single word.
     *
     * @param wordKey the key of the word
     * @return the transalated word
     */
    public String translateWord(final String wordKey) {
        ResourceBundle resourceBundle = getWordResourceBundle();
        TranslationService translationService = new TranslationService(
                resourceBundle, getApplicationLocale());
        return translationService.translate(wordKey);
    }

    /**
     * Retrieves a resource bundle used to translate words.
     *
     * @return the word resource bundle
     */
    public ResourceBundle getWordResourceBundle() {
        return ResourceBundle.getBundle(WORDS_RESOURCE_BUNDLE_PATH);
    }

    /**
     * Translates a faces message used by the Java Server Faces framework.
     *
     * @param facesMessageKey the faces message key
     * @return the transalated faces message
     */
    public String translateFacesMessage(final String facesMessageKey) {
        ResourceBundle resourceBundle = getFacesMessageResourceBundle();
        TranslationService translationService = new TranslationService(
                resourceBundle, getApplicationLocale());
        return translationService.translate(facesMessageKey);
    }

    /**
     * Retrieves a resource bundle used to translate faces messages.
     *
     * @return the faces message resource bundle
     */
    public ResourceBundle getFacesMessageResourceBundle() {
        return ResourceBundle.getBundle(FACES_MESSAGE_RESOURCE_BUNDLE_PATH);
    }

    /**
     * Translates an application status message.
     *
     * @param applicationStatusMessageKey the application status message key
     * @return the translated application status message
     */
    public String translateApplicationStatusMessage(final String applicationStatusMessageKey) {
        ResourceBundle resourceBundle = getApplicationStatusMessagesResourceBundle();
        TranslationService translationService = new TranslationService(
                resourceBundle, getApplicationLocale());
        return translationService.translate(applicationStatusMessageKey);
    }

    /**
     * Retrieves a resource bundle used to translate application status.
     *
     * @return the application status resource bundle
     */
    public ResourceBundle getApplicationStatusMessagesResourceBundle() {
        return ResourceBundle.getBundle(APPLICATION_STATUS_BUNDLE_PATH);
    }

    /**
     * Returns the current {@link Locale} been used to render the view. Uses
     * a default {@link Locale} if the view is not disponible.
     *
     * @see ApplicationConfiguration#locale
     */
    public Locale getApplicationLocale() {
        Locale targetLocale;

        try {
            targetLocale = ApplicationBean.getCurrentInstance().
                    getApplicationConfiguration().getLocale();
        } catch (NullPointerException nullPointerException) {
            targetLocale = ApplicationDefaults.DEFAULT_LOCALE;
        }

        return targetLocale;
    }

    /**
     * Returns the locales supported by the application. Used to apply
     * localization. Needs a active instance of the {@link FacesContext}.
     *
     * @return the supported locales
     */
    public List<Locale> getSupportedLocales() {
        List<Locale> supportedLocales = new ArrayList<Locale>();
        for (Iterator<Locale> it = FacesContext.getCurrentInstance().
                getApplication().getSupportedLocales(); it.hasNext();) {
            Locale locale = it.next();
            supportedLocales.add(locale);
        }
        return supportedLocales;
    }
}
